package com.zhoug.android.common.utils;

import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.Gravity;

/**
 * @Author 35574
 * @Date 2020/4/24
 * @Description
 */
public class SpannableUtils {

    /**
     * 添加必填项的*号标记,并标红(文字的最后)
     * @param required  required
     * @param required  gravity {@link Gravity#START,Gravity#END}
     * @return Spannable
     */
    public static Spannable requiredText(CharSequence required,int gravity){
        if(required!=null){
            String text = required.toString();
            SpannableString spannable;
            //左边添*
            if(gravity==Gravity.START|| gravity==Gravity.LEFT){
                if(!text.startsWith(":")){
                    text="*"+text;
                }
                spannable= new SpannableString(text);
                spannable.setSpan(new ForegroundColorSpan(Color.RED), 0, 1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }else{
                boolean hasMaoHao=false;
                if(text.endsWith(":")){
                    text=text.substring(0,text.length()-1);
                    hasMaoHao=true;
                }
                if(!text.endsWith("*")){
                    text=text+"*";
                }
                int index=text.length()-1;
                if(hasMaoHao){
                    text=text+":";
                    index=text.length()-2;
                }
                spannable= new SpannableString(text);
                spannable.setSpan(new ForegroundColorSpan(Color.RED), index, index+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            }
            return spannable;
        }
        return null;
    }

    /**
     * 添加必填项的*号标记,并标红(文字的右边)
     * @param required  required
     * @return Spannable
     */
    @Deprecated
    public static Spannable requiredText(CharSequence required ){
        if(required!=null){
            String text = required.toString();
            SpannableString spannable;
            //文本中包含*,则*号标红
            int index = text.lastIndexOf("*");
            if(index>=0 && index<text.length()){
                spannable= new SpannableString(text);
                spannable.setSpan(new ForegroundColorSpan(Color.RED), index, index+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

            }else{
                //文本中不包含*
                if(text.endsWith(":")){
                    //冒号结尾
                    spannable= new SpannableString(text.substring(0,text.length()-1)+  "*:");
                    spannable.setSpan(new ForegroundColorSpan(Color.RED), spannable.length() - 2, spannable.length()-1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                }else{
                    //纯文本,添加*
                    spannable= new SpannableString(text+"*");
                    spannable.setSpan(new ForegroundColorSpan(Color.RED), spannable.length()-1, spannable.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                }
            }
            return spannable;
        }
        return null;
    }

    /**
     * 给指定位置的文字添加颜色
     * @param text 文本
     * @param color 颜色
     * @param start 开始位置(包含)
     * @param end 结束位置(不包含)
     * @return Spannable
     */
    public static Spannable setTextColor(String text,int color,int start,int end){
        SpannableString span = new SpannableString(text);
        span.setSpan(new ForegroundColorSpan(color), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        return span;
    }

    /**
     * 给指定位置的文字添加颜色
     * @param text 文本
     * @param proportion 原文字大小的缩放率
     * @param start 开始位置(包含)
     * @param end 结束位置(不包含)
     * @return Spannable
     */
    public static Spannable setTextRelativeSize(String text,float proportion,int start,int end){
        SpannableString span = new SpannableString(text);
        span.setSpan(new RelativeSizeSpan(proportion), start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        return span;
    }

}
